#include <iostream>
#include <vector>
#include <memory>

// 常量定义
constexpr int TRUE = 1;
constexpr int FALSE = 0;
constexpr int OK = 1;
constexpr int ERROR = 0;
constexpr int OVERFLOW = -1;
constexpr int LONGTH = 5;

// 类型定义
using Status = int;
using ElemType = int;

// 顺序表的类型
class SqList {
public:
    SqList(int initialSize = 5, int increment = 5)
        : elem_(std::make_unique<std::vector<ElemType>>(initialSize)),
          length_(0),
          size_(initialSize),
          increment_(increment) {}

    // 初始化顺序表
    Status InitList(int size, int inc) {
        if (size != this->size_ || inc != this->increment_) {
            // 仅在需要时调整大小
            elem_->resize(size);
            this->size_ = size;
            this->increment_ = inc;
        }
        return OK;
    }

    // 销毁顺序表
    Status DestroyList() {
        // 使用智能指针自动管理内存
        return OK;
    }

    // 将顺序表清空
    Status ClearList() {
        length_ = 0;
        return OK;
    }

    // 判断顺序表是否为空
    bool IsEmpty() const {
        return length_ == 0;
    }

    // 返回顺序表中元素个数
    int Length() const {
        return length_;
    }

    // 用e返回顺序表中第i个元素的值
    Status GetElem(int i, ElemType &e) const {
        if (i < 0 || i >= length_) return ERROR;
        e = (*elem_)[i];
        return OK;
    }

    // 在顺序表中查找元素e，成功时返回该元素在表中第一次出现的位置，否则返回 -1
    int Search(ElemType e) const {
        for (int i = 0; i < length_; ++i) {
            if ((*elem_)[i] == e) {
                return i;
            }
        }
        return -1;
    }

    // 遍历顺序表，依次对每个元素调用函数visit()
    Status TraverseList(Status(*visit)(ElemType e)) const {
        if (length_ == 0) return ERROR;
        for (const auto& elem : *elem_) {
            visit(elem);
        }
        return OK;
    }

    // 将顺序表中第i个元素赋值为e
    Status PutElem(int i, ElemType e) {
        if (i < 0 || i >= length_) return ERROR;
        (*elem_)[i] = e;
        return OK;
    }

    // 在顺序表表尾添加元素e
    Status Append(ElemType e) {
        if (length_ >= size_) {
            elem_->resize(size_ + increment_, 0);
            size_ += increment_;
        }
        (*elem_)[length_] = e;
        length_++;
        return OK;
    }

    // 删除顺序表的表尾元素，并用参数e返回其值
    Status DeleteLast(ElemType &e) {
        if (length_ == 0) return ERROR;
        e = (*elem_)[--length_];
        return OK;
    }

    int GetSize() const {return size_;}
    int GetIncrement() const {return increment_;}

private:
    std::unique_ptr<std::vector<ElemType>> elem_;  // 存储表元素
    int length_;                                  // 表的长度
    int size_;                                    // 表的大小
    int increment_;                               // 每次增长的步长
};

// 遍历调用
Status visit(ElemType e) {
    std::cout << e << '\t';
    return OK;
}

int main() {
    SqList L(LONGTH, LONGTH);
    ElemType e, eArray[LONGTH] = {1, 2, 3, 4, 5};

    std::cout << "---【顺序表】---\n";
    std::cout << "表L的size为：" << L.GetSize() << "\n表L的increment为：" << L.GetIncrement() << "\n";
    std::cout << "待测试元素为：\n";
    for (const auto& value : eArray) {
        std::cout << value << '\t';
    }
    std::cout << '\n';

    // 初始化顺序表
    if (!L.InitList(LONGTH, LONGTH)) {
        std::cout << "初始化顺序表失败\n";
        return 1;
    }
    std::cout << "已初始化顺序表\n";

    // 判空
    if (L.IsEmpty()) std::cout << "此表为空表\n";
    else std::cout << "此表不是空表\n";

    // 入表
    std::cout << "将待测元素入表：\n";
    for (const auto& value : eArray) {
        if (L.Append(value) != OK) {
            std::cout << "入表失败\n";
            return 1;
        }
    }
    std::cout << "入表成功\n";

    // 遍历顺序表L
    std::cout << "此时表内元素为：\n";
    L.TraverseList(visit);
    std::cout << '\n';

    // 出表
    std::cout << "将表尾元素出表到e：\n";
    ElemType lastElem;
    if (L.DeleteLast(lastElem) != OK) {
        std::cout << "出表失败\n";
    } else {
        std::cout << "出表成功\n出表元素为" << lastElem << '\n';
    }

    // 遍历顺序表L
    std::cout << "此时表内元素为：\n";
    L.TraverseList(visit);
    std::cout << '\n';

    // 销毁顺序表
    std::cout << "销毁顺序表\n";
    if (L.DestroyList() == OK) {
        std::cout << "销毁成功\n";
    } else {
        std::cout << "销毁失败\n";
    }

    return 0;
}